#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2022-3-2 13:42
# @Author : huabao.tan
# @File : item.py
# @Software: PyCharm


from tool.sql.db import *
from jx3.api import *
from tool.allFunction.Function import *


class WriteMysql(CreatExcel):

    def __init__(self, name):
        super(WriteMysql, self).__init__()
        self.db = DB()
        self.p = Price('%s' % name)

    def write(self):

        data = self.p.goods_json
        if data == None:
            return '没有查询到数据'
        elif len(data) == 0:
            return '没有查询到数据'
        try:
            info = data['data']['info']
            upload = data['data']['upload']
            data = data['data']['data']
            c = 0
            for b in data:
                for a in b:
                    c += 1
                    sql = """INSERT INTO info_item(item_name, type, server, zone, price, type_sales, time, info, upload) value ('%s','%s','%s','%s','%s','%s','%s','%s','%s');""" % (
                        a['name']
                        , a['type'], self.p.dealStr(a['server']), self.p.dealStr(a['zone']),
                        self.p.dealStr(str(a['price']) + 'RMB', 0),
                        a['sales'], a['time'], info, upload)
                    self.db.execute_sql(sql)
                    value = a['name']
                    # self.sheet_row_col_value(self, 0, c, 1, value)
                    # self.open_excel().save(self.filename)
            self.db.commit()
            self.db.close()
        except Exception as e:
            return ''


def que_info():
    data = input('输入要查询内容：')
    p = WriteMysql('%s' % data)
    w = Price('%s' % data)
    w.info()
    p.write()
    print('已经写入到数据库')


que_info()
